package uia.alumni.data;

import java.sql.Date;
import java.util.Properties;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;



/**
 *
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class Populator {
    /** The constructor creates the tables */
    public Populator() {
        Properties props = new Properties();
        props.put("toplink.ddl-generation", "drop-and-create-tables");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Alumni", props);

        //create the database tables
        emf.createEntityManager().close();
        emf.close();
    }


    /** The populate() method populates the database. */
    public void populate() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Alumni");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        
        User even = new User("even", "even");
        even.addRole(Role.admin);
        even.addRole(Role.member);
        even.setAddress("hjemme");
        even.setDescription("En snill gutt...");
        even.setFullName("Even Aaby Larsen");
        even.setPhone("1234 5678");
        even.addCvEntry(new CvEntry("Primary school",
                "Learned a lot",
                new Date(70, 8, 20),
                new Date(76, 6, 20)));
        even.addCvEntry(new CvEntry("Secondary school",
                "Learned more",
                new Date(76, 8, 20),
                new Date(79, 6, 20)));
        even.addCvEntry(new CvEntry("High school",
                "Did _you_ learn anything?",
                new Date(79, 8, 20),
                new Date(82, 6, 20)));
        even.addCvEntry(new CvEntry("University",
                "Learned everything",
                new Date(83, 8, 20),
                new Date(88, 12, 20)));
        even.addCvEntry(new CvEntry("Some experience",
                "Wow!",
                new Date(89, 1, 20),
                new Date(94, 6, 20)));
        even.addCvEntry(new CvEntry("More experience",
                "Learned a lot",
                new Date(94, 8, 20),
                new Date(System.currentTimeMillis())));
        em.persist(even);

        User adm1 = new User("admin", "admin");
        adm1.addRole(Role.admin);
        em.persist(adm1);

        User prof1 = new User("professor", "professor");
        prof1.addRole(Role.member);
        em.persist(prof1);

        User ass1 = new User("assistant", "assistant");
        ass1.addRole(Role.member);
        em.persist(ass1);

        User stud1 = new User("student", "student");
        stud1.addRole(Role.member);
        em.persist(stud1);


        em.getTransaction().commit();
        em.close();
        emf.close();
    }


    public static void main(String[] args) throws Exception {
        System.getProperties().list(System.out);

        try {
            Populator m = new Populator();
            m.populate();
        }
        catch (Exception e) {
            e.printStackTrace(System.out);
            throw e;
        }
    }

}
